﻿using System;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class ctlSelectSeat : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public int RetreiveSeatMap(long bk_id, int pax_sn, int trip_sn, string user_airline, string office, bool low_priority)
        {
            int ret_code;
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "bll_tkt_get_trip_seat_map";
            SqlParameter para = cmd.Parameters.AddWithValue("@bk_id", bk_id);
            para = cmd.Parameters.AddWithValue("@pax_sn", pax_sn);
            para = cmd.Parameters.AddWithValue("@trip_sn", trip_sn);
            para = cmd.Parameters.AddWithValue("@user_airline", user_airline);
            para = cmd.Parameters.AddWithValue("@office", office);
            DataTable dtMap = new DataTable();
            try
            {
                ret_code = TinyDAL.Exec(ref cmd, ref dtMap);
                switch (ret_code)
                {
                    case 0:
                        //lblError.Text = "OK.";
                        break;
                    case 1:
                        //lblError.Text = "Ticket not found, or all tickets in the order are checked-in!";
                        return ret_code;
                    case 2:
                        //lblError.Text = "Seat map not found!";
                        return ret_code;
                    case 50:
                        //lblError.Text = "Unauthorized airline!";
                        return ret_code;
                    default:
                        //lblError.Text = "Error:" + ret_code.ToString();
                        return ret_code;
                }
                tabSeatMap.Rows.Clear();
                TableRow tempRow = new TableRow();
                TableCell tempCell;
                Int16 last_col = -1;
                string space_type, seat_status/*, inf_seat*/;
                int curr_x = 1;
                foreach (DataRow row in dtMap.Rows)
                {
                    if (last_col != Convert.ToInt16(row["y"])/* || last_col == max_y + 1*/)
                    {
                        tabSeatMap.Rows.Add(tempRow);
                        if (last_col - 1 != Convert.ToInt16(row["y"]))
                        {
                            for (last_col--; last_col > Convert.ToInt16(row["y"]); last_col--)
                            {
                                tempRow = new TableRow();
                                tempCell = new TableCell();
                                tempCell.HorizontalAlign = HorizontalAlign.Center;
                                tempCell.Text = "<b>" + last_col.ToString() + "</b>";
                                tempRow.Cells.Add(tempCell);
                                tabSeatMap.Rows.Add(tempRow);
                            }
                        }

                        tempRow = new TableRow();
                        last_col = Convert.ToInt16(row["y"]);
                        tempCell = new TableCell();
                        tempCell.Text = "<b>" + last_col.ToString() + "</b>";
                        tempCell.HorizontalAlign = HorizontalAlign.Center;
                        tempRow.Cells.Add(tempCell);

                        curr_x = 1;
                    }
                    if (curr_x != Convert.ToInt16(row["x"]))
                    {
                        for (; curr_x < Convert.ToInt16(row["x"]); curr_x++)
                        {
                            tempCell = new TableCell();
                            tempCell.Text = "&nbsp;";
                            tempRow.Cells.Add(tempCell);
                        }
                    }
                    //if (last_col != Convert.ToInt16(row["y"]) && last_col != -1)
                    //{
                    //    tabSeatMap.Rows.Add(tempRow);
                    //    tempRow = new TableRow();
                    //    last_col = Convert.ToInt16(row["y"]);
                    //}
                    //if (last_col == -1)
                    //{
                    //    last_col = Convert.ToInt16(row["y"]);
                    //}

                    tempCell = new TableCell();
                    space_type = row["cell_type"].ToString();
                    string buf1, buf2, buf3;
                    if (space_type == "S")//seat
                    {
                        seat_status = row["status"].ToString();
                        //if (row["attributes"].ToString().IndexOf('I') >= 0)
                        //    inf_seat = "(I)";
                        //else
                        //    inf_seat = "";
                        
                        //buf2 = "type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["row"].ToString() + row["col"].ToString() + "|" + row["cabin"].ToString() + "|" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd();
                        buf1 = "<input name='SEAT' type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'";
                        buf3 = " title='" + row["row"].ToString() + row["col"].ToString() + "|" + row["cabin"].ToString() + "|" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "'>";
                        switch (seat_status)
                        {
                            case "A"://available
                                if ((row["ori_airport"].ToString().TrimEnd() == "*" || row["ori_airport"].ToString().TrimEnd() == row["pax_ori_airport"].ToString().TrimEnd())
                                    && (row["des_airport"].ToString().TrimEnd() == "*" || row["des_airport"].ToString().TrimEnd() == row["pax_des_airport"].ToString().TrimEnd())
                                    && (row["pax_cabin"].ToString() == row["cabin"].ToString())
                                    && (row["class"].ToString().TrimEnd() == "*" || row["class"].ToString().TrimEnd() == row["pax_class"].ToString().TrimEnd())
                                    && (row["seat_type"].ToString().TrimEnd() == "*" || row["seat_type"].ToString().TrimEnd() == row["pax_seat_type"].ToString().TrimEnd())
                                    /*&& (!Convert.ToBoolean(row["hold_inf"]) || Convert.ToBoolean(row["hold_inf"]) && inf_seat!="")*/)  //考虑到报婴座位稀缺，由后台人员通过锁位管理，不自动处理！那么旅客名单中报婴标志就没什么用处了。
                                {
                                    //buf1 = "<input name='SEAT' style='background-color:lightgreen' ";
                                    ////tempCell.Text = "<input name='SEAT' style='background-color:green' type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString() + row["col"].ToString();
                                    //tempCell.BackColor = System.Drawing.Color.LightGreen;
                                    buf2 = "><img src='images/seat_avail.png'";
                                }
                                else
                                {
                                    //buf1 = "<input name='SEAT' style='background-color:Magenta;' ";//red
                                    if (low_priority)
                                        buf1 = buf1 + "disabled";   //低优先级的旅客预选座位，不允许跨规则选位！

                                    ////tempCell.Text = "<input name='SEAT' style='background-color:yellow' type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString() + row["col"].ToString();
                                    //tempCell.BackColor = System.Drawing.Color.Magenta;
                                    buf2 = "><img src='images/seat_reserved.png'";
                                }
                                
                                break;
                            case "O"://occupied
                                
                                if (Convert.IsDBNull(row["ori_row"]) || Convert.IsDBNull(row["ori_col"]) || Convert.ToInt16(row["ori_row"]) != Convert.ToInt16(row["row"]) || row["ori_col"].ToString() != row["col"].ToString())
                                {
                                    //buf1 = "<input name='SEAT' style='background-color:Cyan' disabled "; //blue
                                    //tempCell.BackColor = System.Drawing.Color.Cyan;
                                    buf2 = "><img src='images/seat_occupied.png'";
                                }
                                else if (Convert.ToInt16(row["ori_row"]) == Convert.ToInt16(row["row"]) && row["ori_col"].ToString() == row["col"].ToString())
                                {
                                    //buf1 = "<input name='SEAT' style='background-color:Gold' ";  //可重复选自己已选的座位，以便取消预选座位
                                    //tempCell.BackColor = System.Drawing.Color.Gold; //yellow
                                    buf2 = "><img src='images/seat_mine.png'";
                                }
                                else //不可能了吧？
                                { 
                                    //buf1 = "<input name='SEAT' style='background-color:gray' disabled ";
                                    //tempCell.BackColor = System.Drawing.Color.Gray;
                                    buf2 = "><img src='images/seat_gray.png'";
                                }
                                //tempCell.Text = "<input name='SEAT' style='background-color:red' disabled type='radio' value='" + row["row"].ToString() + "|" + row["col"].ToString() + "'>" + row["class"].ToString().TrimEnd() + "|" + row["seat_type"].ToString().TrimEnd() + "|" + row["row"].ToString().Trim() + row["col"].ToString();
                                break;
                            //case "B":
                            //    //tempCell.Text = "[B]" + row["row"].ToString() + row["col"].ToString();
                            //    tempCell.Text = "<input name='SEAT' type='radio' value='" /*+ row["tail_no"].ToString() + "|" + row["airline"].ToString() + "|" + row["flt_date"].ToString() + "|" + row["flt_no"].ToString() + "|" */+ row["row"].ToString() + "|" + row["col"].ToString() + "'><font color='red'>" + space_type + "|" + row["row"].ToString() + row["col"].ToString() + "</font>";
                            //    break;
                            default://impossible!
                            { 
                                //buf1 = "<input name='SEAT' style='background-color:gray' disabled ";
                                ////tempCell.Text = row["row"].ToString() + row["col"].ToString()+"?";
                                //tempCell.BackColor = System.Drawing.Color.Gray;
                                buf2 = "><img src='images/seat_gray.png'";
                                break;
                            }
                        }
                        tempCell.Text = "&nbsp;" + buf1 + buf2 + buf3;

                    }
                    else//=='R' //Road
                    {
                        tempCell.Text = "&nbsp;";
                    }
                    tempRow.Cells.Add(tempCell);
                    curr_x = Convert.ToInt16(row["x"]) + 1;
                }
                dtMap.Clear();
                tabSeatMap.Rows.Add(tempRow);

                tempRow = new TableRow();
                tempCell = new TableCell();
                tempCell.Text = "<b>Y/X</b>";
                tempRow.Cells.Add(tempCell);
                for (ret_code = 1; ret_code <= curr_x - 1; ret_code++)
                {
                    tempCell = new TableCell();
                    tempCell.Text = "&nbsp;<b>" + ret_code.ToString() + "</b>";
                    //tempCell.Width = System.Web.UI.WebControls.Unit.Pixel(200);
                    tempCell.HorizontalAlign = HorizontalAlign.Center;
                    tempRow.Cells.Add(tempCell);
                }
                tabSeatMap.Rows.Add(tempRow);

                return 0;
            }
            catch (Exception ex)
            {
                return 100;
                //if (Application["DEBUG"].ToString() == "1")
                //lblError.Text = ex.Message;
                //else
                //lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}